package com.phuc.game.tankshooter.sprite;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.constants.TimeConstants;

import com.phuc.game.tankshooter.MainActivity;

public class ExplodeSprite extends AnimatedSprite implements TimeConstants {

	public static final int WIDTH = 100;
	public static final int HEIGHT = 100;
	public static final int EXPLODE_SIZE_SMALL = 1111;
	public static final int EXPLODE_SIZE_NORMAL = 2222;
	public static final int EXPLODE_SIZE_LARGE = 3333;
	
	Scene scene;

	public ExplodeSprite(float pX, float pY, float pTileWidth, float pTileHeight,
			TiledTextureRegion pTiledTextureRegion, Scene scene) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);
		this.scene = scene;
		settingMovePath((int) pX, (int) pY);
	}

	private ExplodeSprite getInstance() {
		return this;
	}

	public void settingMovePath(int pX, int pY) {
		final int x1 = pX;
		final int y1 = pY;

		int startFrame = 0;
		int endFrame = 5;
		int frameDuration = 100;
		getInstance().animate(
			new long[] { frameDuration, frameDuration,
					frameDuration, frameDuration,
					frameDuration, frameDuration }, startFrame, endFrame,
			0, new IAnimationListener() {
				
				@Override
				public void onAnimationEnd(final AnimatedSprite pAnimatedSprite) {
					MainActivity.getInstant().runFromUpdateThread(new Runnable() {
						@Override
						public void run() {
							MainActivity.getInstant().getGamePlay().createSmoke(x1 + WIDTH / 2, y1 + HEIGHT / 2, 0);
							pAnimatedSprite.detachSelf();
							pAnimatedSprite.setIgnoreUpdate(true);
							pAnimatedSprite.clearEntityModifiers();
							pAnimatedSprite.clearUpdateHandlers();
						}
					});
				}
				
			});
	}
	
	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
	}
}
